/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "ProjectDialog.h"


namespace QSThink
{
	ProjectDialog::ProjectDialog(QWidget *parent)
		: QDialog(parent),_ui(new Ui::ProjectDialogUI)
	{
		_ui->setupUi(this);

		_projectListModel=new QStringListModel(this);
		_ui->listView->setModel(_projectListModel);
		
		QString locPath=QDir::currentPath()+"/Projects";
		_ui->pathComboBox->addItem(locPath);
		_ui->pathComboBox->setEditText(locPath);

		connect(_ui->browseButton, SIGNAL(clicked()), this, SLOT(createFileDialog()));
		connect(_ui->tabWidget,SIGNAL(currentChanged(int)),this,SLOT(changeTab(int)));
	}

	ProjectDialog::~ProjectDialog()
	{
		delete _ui;
	}

	void ProjectDialog::selectCreateTab()
	{
		_ui->tabWidget->setCurrentIndex(0);
	}

	void ProjectDialog::selectOpenTab()
	{
		_ui->tabWidget->setCurrentIndex(1);
	}

	

	void ProjectDialog::changeTab(int index)
	{
		if(index==1){//open tab
			
			//QStringList nameList=getProjectPathes();
			//_projectListModel->setStringList(nameList);
		}
	}

	void ProjectDialog::createFileDialog()
	{
		QString path=_ui->pathComboBox->currentText();
		QString dir = QFileDialog::getExistingDirectory(this, QObject::tr("Open Directory"),
                                                 path,
                                                 QFileDialog::ShowDirsOnly
                                                 | QFileDialog::DontResolveSymlinks);
		if(dir==path){
			return;
		}
		if(!dir.isEmpty()){
			bool add=true;
			for(int i=0;i<_ui->pathComboBox->count();i++){
				if(_ui->pathComboBox->itemText(i)==dir){
					add=false;
					break;
				}
			}

			if(add){
				_ui->pathComboBox->addItem(dir);
			}

			_ui->pathComboBox->setEditText(dir);
			
		}
	}

	void ProjectDialog::createProject()
	{
		/*
		QString dirPath=_ui->pathComboBox->currentText();
		if(!QDir::isAbsolutePath(dirPath)){
			QMessageBox box;
			 box.setText(QObject::tr("The path is not legal."));
			 box.exec();
			 return;
		}

		QString name=_ui->nameEdit->text();
		if(name.isEmpty()){
			QMessageBox box;
			 box.setText(QObject::tr("The name can only use letter."));
			 box.exec();
			 return;
		}
		
		if(GNgProjectManager.createProject(dirPath.toStdString(),name.toStdString())){
			
			ApplicationSingleton.loadProject(name);
			
			this->close();
		}else{
			QMessageBox box;
			box.setText(QObject::tr("Can't create project."));
			box.exec();
		}
		*/
	}

	void ProjectDialog::loadProject()
	{
		/*
		QModelIndex index= _ui->listView->currentIndex();
		if(index.isValid()){
			
			GNgProjectManager.loadProject(index.row());
			
			QString name=QString::fromStdString(GNgProjectManager.getProjectName(index.row()));
			ApplicationSingleton.loadProject(name);

			close();//close dialog if successed
		}else{
			QMessageBox box;
			box.setText(QObject::tr("Please select a project or create a new."));
			box.exec();
		}
		*/
	}

	void ProjectDialog::accept()
	{
		int index=_ui->tabWidget->currentIndex();
		switch(index){
			case 0:
				createProject();
				break;
			case 1:
				loadProject();
				break;
			default:
				break;
		}
		QDialog::accept();
	}

	void ProjectDialog::reject()
	{
		QDialog::reject();
	}

}
